"use strict"
const { segment } = require("icqq")
const { bot } = require("../index")

let Config = new class {
    constructor() {
        this.GroupId = [111,];//需要管理的群
        this.Admin_Id = 1;//主人QQ
    }
    async sleep(ms) {
        return new Promise((resolve) => setTimeout(resolve, ms))
    }
}

// 违禁词惩罚
bot.on("message.group", async function sensitive(msg) {
    let Prohibited_Words = [/彩票/g, /资料墙/g, /校园墙/g];
    var length = Prohibited_Words.length;
    let qq = Number(msg.user_id)
    for (var i = 0; i < length; i++) {
        if (Prohibited_Words[i].test(msg.raw_message) && Config.GroupId.includes(msg.group_id)) {
            msg.group.recallMsg(msg);
            msg.group.muteMember(qq, 86400);//禁言的时间单位为秒
            let remsg = [
                segment.at(qq),
                `由于你触发群内关键词，你将被禁言1天，如有疑问，请直接私信群主`,
            ];
            msg.reply(remsg);
            return true;
        }
    }
})
//给予头衔
bot.on("message.group", async function Give_Title(msg) {
    let flag = 0;
    let str = msg.raw_message;
    let regtext = /^我要头衔(.*?)/;
    let Prohibited_Words = [/爸/g, /爹/g, /爷/g, /群主/g, /管理/g];//头衔违禁词
    var length = Prohibited_Words.length;
    let qq = Number(msg.user_id);
    let title = str.slice(4, str.length);
    if (regtext.test(str) && Config.GroupId.includes(msg.group_id)) {
        if (msg.group.is_owner) {
            if (title.length > 6 || title.length <= 0) {
                msg.reply(`头衔最大长度为6，请重新输入`, true);
                return true;
            } else {
                for (var i = 0; i < length; i++) {
                    if (Prohibited_Words[i].test(title) && Config.GroupId.includes(msg.group_id)) {
                        msg.group.recallMsg(msg);
                        msg.group.muteMember(qq, 1800);
                        flag = 1;
                        msg.group.setTitle(qq, '');
                        let remsg = [
                            segment.at(qq),
                            `你申请的头衔中包含违禁词，你将被禁言30分钟，并回收你目前的头衔,有疑问请联系群主`,
                        ];
                        msg.reply(remsg)

                        return true;
                    }
                }
            }
            if (flag == 0) {
                msg.group.setTitle(qq, title);
                msg.reply(`已为你成功将头衔设置为${title}`, true);
                return true;
            }
        }
        else {
            msg.reply(`我不是群主，无法进行头衔的操作`, true);
        }
    }
    else if (msg.raw_message == "我不要头衔了") {
        msg.group.setTitle(qq, '');
        msg.reply(`头衔撤销成功了`, true);
        return true;
    }
})

//给予管理
bot.on("message.group", async function SetAdmin(msg) {
    let regtext = /^设置管理(.*?)/;
    if (regtext.test(msg.raw_message) && Config.Admin_Id == msg.user_id && Config.GroupId.includes(msg.group_id)) {
        let qq = null
        for (let e of msg.message) {
            if (e.type === 'at') {
                qq = e.qq
                break;
            }
        }
        if (qq == null) {
            msg.reply(`未指定需要设置管理的对象`, true);
        } else {
            if (msg.group.pickMember(qq).is_admin) {
                msg.reply(`此人已经是本群的管理了，无法再次设置`, true)
            }
            else {
                msg.group.setAdmin(qq, 1);
                let remsg = [
                    `已经成功将`,
                    segment.at(qq),
                    `设置为本群管理了`,
                ];
                msg.reply(remsg, true);
            }
        }
    }
})

//撤销管理
bot.on("message.group", async function RepealAdmin(msg) {
    let regtext = /^撤销管理(.*?)/;
    if (regtext.test(msg.raw_message) && Config.Admin_Id == msg.user_id && Config.GroupId.includes(msg.group_id)) {
        let qq = null
        for (let e of msg.message) {
            if (e.type === 'at') {
                qq = e.qq
                break;
            }
        }
        if (qq == null) {
            msg.reply(`未指定需要撤销管理的对象`, true);
        } else {
            if (Config.Admin_Id == msg.user_id) {
                console.log(qq)
                msg.group.setAdmin(qq, 0);
                let remsg = [
                    `已经成功将`,
                    segment.at(qq),
                    `的管理撤销了`,
                ];
                msg.reply(remsg, true);
            }
            else {
                msg.reply(`此人并不是本群的管理，无法进行撤销`, true)
            }
        }
    }
})

//踢出群成员
bot.on("message.group", async function KickaMember(msg) {
    let regtext = /^踢(.*?)/;
    if (regtext.test(msg.raw_message) && Config.GroupId.includes(msg.group_id)) {
        if (msg.user_id == Admin_Id) {
            let kick_qq = null
            for (let e of msg.message) {
                if (e.type === 'at') {
                    kick_qq = e.qq
                    console.log(kick_qq);
                    break;
                }
            }
            let nickname = await (msg.group.pickMember(kick_qq).nickname || msg.group.pickMember(kick_qq).card)
            if (kick_qq == null) {
                msg.reply(`未指定需要踢出的对象`);
            }
            else {
                msg.group.kickMember(kick_qq);
                msg.reply(`已经成功将${nickname}(${kick_qq})踢出本群`, true);
            }
        }
    }
})
